<?php
class User extends AppModel {

  public $name = 'User';
  public $hasAndBelongsToMany = array(
      'Role'=> array(
          'className'=>'Role',
          'joinTable'=>'user_roles',
          'foreign_key'=>'user_id',
          'associationForeignKey'=>'role_name',
          'unique'=>true
      )
  );
  public $validate = array(
      'first_name' => array(
          'rule' => 'notEmpty',
          'message' => 'First name is required'
      ),
      'last_name' => array(
          'rule' => 'notEmpty',
          'message' => 'Last name is required'
      ),
      'email'=>array(
          'required' => array(
            'rule' => array('email',true),
            'allowEmpty'=> false,
            'required'=>true,
            'message'=>'Specify a valid email address'
          ),
          'valid' => array(
              'rule'    => array('isValidEmail'),
              'message' => 'This email address has already been registered. Use another email address.'
          )          
      ),
      'password' => array(
          'required' => array(
              'rule'     => 'notEmpty',
              'required' => true,
              'message'  => 'Password is required'
          ),
          'minlength' => array(
              'rule'    => array('minLength', 5),
              'message' => 'Password must be at least 5 characters'
          )
      )
  );

  function isValidEmail($field) {
    $retVal = false;
    if (!empty($field)){
      $user = new User();
      $result = $user->find("first",array('conditions'=>array("User.email"=>$this->data['User']['email'])));
      $retVal = !empty($result)? false : true;
    }
    return $retVal;
  }


}

?>
